package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.unionwar.BaseUnionGuessInfo;
import com.yanqu.road.entity.unionwar.ConditionAchieveInfo;
import com.yanqu.road.entity.unionwar.UserUnionWarGuessData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.unionwar.UnionWarActivityModule;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_UNION_WAR_ACTIVITY_GET_CONDITION_ACHIEVE, desc = "获取奖励达成玩家数量")
public class UnionWarGetConditionAchieveCmd extends AbstractCommand {
    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.GetConditionAchieveReqMsg msg = UnionWarActivityServerProto.GetConditionAchieveReqMsg.parseFrom(packet.getBytes());
        activityId = msg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.GetConditionAchieveRespMsg.Builder respMsg = UnionWarActivityServerProto.GetConditionAchieveRespMsg.newBuilder();
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWar(activityId);
        if (unionWar != null) {

            for (int phase = 1; phase <= 2; phase++) {
                int conditionType = 1003;
                if (phase == 1) {
                    conditionType = 1002;
                }

                List<Integer> conditionIdList = new ArrayList<>();
                List<ActivityConditionInfo> activityConditionInfoList = UnionWarActivityMgr.getActivityConditionInfoMap().get(activityId);
                for (ActivityConditionInfo conditionInfo : activityConditionInfoList) {
                    if(conditionInfo.getType() == conditionType) {
                        conditionIdList.add(conditionInfo.getConditionId());
                    }
                }
                int myGroupId = GameUnionActivityGroupMgr.getViewDataGroupId(activityId, packet.getPlayerId());
                //根据我的竞猜数据拿分组
                //我的竞猜数据
                UserUnionWarGuessData guessData = player.getModule(UnionWarActivityModule.class).getGuessData(activityId, phase);
                if (guessData != null) {
                    for (BaseUnionGuessInfo guessUnionBaseInfo : guessData.getGuessUnionBaseInfos()) {
                        int tempGroupId = GameUnionActivityGroupMgr.getGroupId(activityId, guessUnionBaseInfo.getUnionUid());
                        if (tempGroupId != myGroupId) {
                            UnionWarActivityMgr.getLogger().info("activityId {} userId {} myGroupId {} guessDataId {}", activityId, player.getUserId(), myGroupId, tempGroupId);
                            myGroupId = tempGroupId;
                            break;
                        }
                    }
                }
                Map<Integer, ConditionAchieveInfo> conditionAchieveInfoMap = unionWar.getConditionAchieveInfoMap().get(myGroupId);
                if(conditionAchieveInfoMap != null) {
                    for (ConditionAchieveInfo achieveInfo : conditionAchieveInfoMap.values()) {
                        if(!conditionIdList.contains(achieveInfo.getConditionId())){
                            continue;
                        }
                        UnionWarActivityServerProto.ConditionAchieveTemp.Builder temp = UnionWarActivityServerProto.ConditionAchieveTemp.newBuilder();
                        temp.setConditionId(achieveInfo.getConditionId());
                        temp.setNum(achieveInfo.getAchieveCount());
                        for (UserBaseInfo userBaseInfo : achieveInfo.getUserList()) {
                            temp.addPlayerBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                        }
                        respMsg.addConditionAchieve(temp);
                    }
                }
            }
        }
        player.sendPacket(ClientProtocol.U_UNION_WAR_ACTIVITY_GET_CONDITION_ACHIEVE, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
